package com.szewec.ps.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.szewec.ps.R;

public class CollapseButton extends ConstraintLayout {
    private TextView collapseBtn;

    public CollapseButton(Context context) {
        super(context);
    }

    public CollapseButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CollapseButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.collapse_button, this);
        collapseBtn = findViewById(R.id.collapse_btn);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.collapseButton);
            boolean isCollapse = ta.getBoolean(R.styleable.collapseButton_isCollapse, true);
            if (isCollapse) {
                collapseBtn.setText(getResources().getString(R.string.show_more));
                collapseBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.arrow_down, 0);
            } else {
                collapseBtn.setText(getResources().getString(R.string.collapse));
                collapseBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.arrow_up, 0);
            }
            ta.recycle();
        }
    }

    /**
     * 更改按钮状态
     * @param status
     */
    public void changeCollapseBtnStatus(Boolean status) {
        if (status) {
            collapseBtn.setText(getResources().getString(R.string.show_more));
            collapseBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.arrow_down, 0);
        } else {
            collapseBtn.setText(getResources().getString(R.string.collapse));
            collapseBtn.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.arrow_up, 0);
        }
    }

    /**
     * 获取折叠按钮的状态， true为折叠状态， false为展开状态
     */
    public Boolean getCollapseBtnStatus() {
        return getResources().getString(R.string.show_more).equals(collapseBtn.getText().toString());
    }
}
